/*
 * string_type.c
 *
 *  Created on: 2019年5月7日
 *      Author: Administrator
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
 * 在c语言中，字符串本质是一维字符数组，以'\0'字符结尾
 */
void string() {

	// 字符串创建
	char greeting[] = "hello";
	printf("Greeting message: %s \n", greeting);

	// 创建方式2
	char *address = (char *)calloc(100, sizeof(char));
	if (address == NULL) {
		exit(1);
	}

	// 两种均可
	address = "test";
//	strcpy(address, "test");
	printf("address: %s", address);

	// 遍历
	int arr_length = sizeof(greeting) / sizeof(greeting[0]);
	for (int i=0; i<arr_length; i++) {

		printf("%p \t", &greeting[i]);
	}

	// 字符串函数
	// strlen 字符串长度，不包括末尾的\0
	int greeting_len = strlen(greeting);
	printf("greeting length: %d \n", greeting_len);

	char string_copied[100];
//	string_copied = "strcpy assigiment"; // 不可以
	strcpy(string_copied, "strcpy from greeting");
	printf("strcpy: %s \n", string_copied);

	strcat(greeting, string_copied);
	printf("strcat: %s \n", greeting);


}
